<?php

namespace App\Exports\Traits;

use PhpOffice\PhpSpreadsheet\Style\Alignment;

trait ExcelExportTrait
{

    //
    public function setExcelDefault($activeSheet){
        $this->setColumnWidth($activeSheet);
        $this->setRowHeight($activeSheet);
        $this->setVertical($activeSheet,'a1:z100');
    }
    //设置列宽
    public function setColumnWidth($activeSheet){
        $width=16;
        $activeSheet->getDefaultColumnDimension()->setWidth($width);
        /*foreach ($this->excelColumns() as $column){
            $activeSheet->getColumnDimension($column)->setWidth($width);
        }*/
    }
    //列宽
    public function excelColumns(){
        return ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    }

    //默认行高
    public function setRowHeight($activeSheet){
        $height=24;
//        $activeSheet->getDefaultRowDimension()->setRowHeight($height);
        for ($i=1;$i<=100;$i++){
            $activeSheet->getRowDimension($i)->setRowHeight(24);
        }
    }

    //默认居中方式
    public function setVertical($activeSheet,$cells){
        //样式设置 - 水平、垂直居中
        $styleArray = [
            'alignment' => [
                'vertical' => Alignment::VERTICAL_CENTER
            ],
        ];
        $activeSheet->getStyle($cells)->applyFromArray($styleArray);
    }
    //默认居中方式
    public function setHorizontal($activeSheet,$cells){
        //样式设置 - 水平、垂直居中
        $styleArray = [
            'alignment' => [
                'horizontal' => Alignment::HORIZONTAL_CENTER,
            ],
        ];
        $activeSheet->getStyle($cells)->applyFromArray($styleArray);
    }

}
